#!/usr/bin/perl
#use strict;
use warnings;

# Script to process all *.txt files in current directory and put the output
# in the corresponding *.txt.new files in the current directory again.

my $indir  = "/Users/kimmasterson/";    # the input directory name
my $outdir = "";                        # the output directory name
my $i      = 0;                         # loop index
my $infile;                             # the input file name
my $outfile;                            # the output file name
my $word;
my $start;

while ( defined( $infile = glob( $indir . "*.html" ) ) )
{    # loop through the txt files in directory $indir
	print $infile;
	$outfile =
	    $outdir . ""
	  . substr( $infile, rindex( $infile, "\\" ) + 1 )
	  . ".txt";    # name the output file
	open( IFILE, "<$infile" )
	  or die "Can't open $infile: $!";    # open input file for reading
	open( OFILE, ">$outfile" )
	  or die "Can't create file: $!";  # create output file for writing
	while (<IFILE>) {                  # loop through contents of the input file

		my @raw_data = <IFILE>;

		foreach $word (@raw_data) {
			if ( $word =~ /<title>/ ) {
				$start = 1;
			}
			if ($start) {
				print OFILE $word;
			}
			if ( $word =~ /<\/title>/ ) {
				$start = 0;
			}
		}
		foreach $word (@raw_data)

		{
			if ( $word =~ /date/ ) {
				$start = 1;
			}

			if ($start) {

				print OFILE $word;

			}

			if ( $word =~ /date/ ) {

				$start = 0;
			}
		}
		foreach $word (@raw_data)

		{
			if ( $word =~ m/div class="body"/ ) {
				$start = 1;
			}

			if ($start) {

				print OFILE $word;

			}

			if (   $word =~ m/id="articleAuthor"/
				|| $word =~ m/div class="toolsBottom"/ )
			{

				$start = 0;
			}
		}
		foreach $word (@raw_data)

		{
			if ( $word =~ m/c3:"",/ ) {
				$start = 1;
			}

			if ($start) {

				print OFILE $word;

			}

			if ( $word =~ m/c5/ ) {

				$start = 0;
			}

		}
		foreach $word (@raw_data)

		{

			if ( $word =~ m/div class="toolsBottom/ ) {

				$start = 0;
			}
		}

	}

	close(IFILE) or die "Can't close $infile: $!";     # close input file
	close(OFILE) or die "Can't close $outfile: $!";    # close output file
	printf( "Output file => %s\n", $outfile );
}
